package com.haibara.codesubmiteval.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.haibara.codesubmiteval.annotation.AuthCheck;
import com.haibara.codesubmiteval.common.BaseResponse;
import com.haibara.codesubmiteval.common.DeleteRequest;
import com.haibara.codesubmiteval.common.ErrorCode;
import com.haibara.codesubmiteval.constant.AuthConstant;
import com.haibara.codesubmiteval.exception.BusinessException;
import com.haibara.codesubmiteval.model.dto.questionCase.QuestionCaseAddRequest;
import com.haibara.codesubmiteval.model.dto.questionCase.QuestionCaseEditRequest;
import com.haibara.codesubmiteval.model.dto.questionCase.QuestionCaseQueryRequest;
import com.haibara.codesubmiteval.model.entity.QuestionCase;
import com.haibara.codesubmiteval.service.QuestionCaseService;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: mengyu
 * @Date: 2025/2/5
 */
@RestController
@RequestMapping("/question_case")
@Slf4j
public class QuestionCaseController {
    @Resource
    private QuestionCaseService questionCaseService;

    /**
     * 测试用例查询请求
     *
     * @param questionCaseQueryRequest 测试用例分页请求
     * @return 测试用例分页信息
     */
    @PostMapping("/page")
    @AuthCheck(mustRole = AuthConstant.ADMIN)
    public BaseResponse<Page<QuestionCase>> pageQueryQuestionCase(@RequestBody QuestionCaseQueryRequest questionCaseQueryRequest) {
        Page<QuestionCase> result = questionCaseService.pageQuestionCase(questionCaseQueryRequest);
        return BaseResponse.success(result);
    }

    /**
     * 新增测试用例
     *
     * @param questionCaseAddRequest 新增测试用例请求
     * @return 执行结果
     */
    @PostMapping("/add")
    @AuthCheck(mustRole = AuthConstant.ADMIN)
    public BaseResponse<Boolean> addQuestionCase(@RequestBody @Valid QuestionCaseAddRequest questionCaseAddRequest) {
        questionCaseService.addQuestionCase(questionCaseAddRequest);
        return BaseResponse.success(true);
    }

    /**
     * 删除测试用例
     *
     * @param deleteRequest 删除请求
     * @return 执行信息
     */
    @DeleteMapping
    @AuthCheck(mustRole = AuthConstant.ADMIN)
    public BaseResponse<Boolean> deleteQuestionCase(@RequestBody @Valid DeleteRequest deleteRequest) {
        questionCaseService.deleteQuestionCase(deleteRequest);
        return BaseResponse.success(true);
    }

    /**
     * 修改测试用例
     *
     * @param questionCaseEditRequest 测试用例修改请求
     * @return 执行信息
     */
    @PutMapping
    @AuthCheck(mustRole = AuthConstant.ADMIN)
    public BaseResponse<Boolean> updateQuestionCase(@RequestBody QuestionCaseEditRequest questionCaseEditRequest) {
        questionCaseService.updateQuestionCase(questionCaseEditRequest);
        return BaseResponse.success(true);
    }

    /**
     * 获取测试用例
     *
     * @param id 测试用例id
     * @return 测试用例信息
     */
    @GetMapping("/get")
    @AuthCheck(mustRole = AuthConstant.ADMIN)
    public BaseResponse<QuestionCase> getQuestionCase(Long id) {
        if (id == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "题目id不能为空");
        }
        QuestionCase questionCase = questionCaseService.getById(id);
        if (questionCase == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR, "题目不存在");
        }
        return BaseResponse.success(questionCase);
    }
}
